package com.csweimei.rentmanagementtreasure.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

public class SemiCircleProgressBar extends View {

    private Paint paint;
    private int progress = 0;
    private int max = 100;
    private int radius;
    private RectF rectF;

    public SemiCircleProgressBar(Context context) {
        super(context);
        init();
    }

    public SemiCircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SemiCircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10); // 设置进度条的宽度
        paint.setColor(Color.BLUE); // 设置进度条颜色
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        radius = Math.min(width, height) / 2;
        rectF = new RectF(-radius, -radius, radius, radius);

        // 先画半圆背景
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.LTGRAY);
        canvas.drawCircle(0, 0, radius, paint);

        // 再画渐变效果
        paint.setShader(new SweepGradient(0, 0, new int[]{Color.TRANSPARENT, Color.BLUE}, null));

        // 画半圆进度
        canvas.drawArc(rectF, -90, ((float) progress / max) * 180, false, paint);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setMax(int max) {
        this.max = max;
    }
}